package com.neuedu.nep.controller;

import com.neuedu.nep.base.BaseController;
import com.neuedu.nep.base.R;
import com.neuedu.nep.dto.GridmemberSignInDTO;
import com.neuedu.nep.model.GridMember;
import com.neuedu.nep.service.GridMemberService;
import com.neuedu.nep.util.JwtUtil;
import com.neuedu.nep.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 黄宇佳
 * @create 2024-04-11 11:44
 * @description:
 */
@RestController
@CrossOrigin
@RequestMapping("gridmember")
public class GridMemberController extends BaseController {
    @Resource
    private GridMemberService gridMemberService;

    @PostMapping("SignIn")
    public R SignIn(@RequestBody GridmemberSignInDTO gridmemberSignInDTO){
        String gmCode=gridmemberSignInDTO.getGmCode();
        String password=gridmemberSignInDTO.getPassword();
        GridMember gridMember = gridMemberService.SignIn(gmCode,password);
        if (gridMember!=null){
            gridMember.setPassword(null);
            UserVO userVO=new UserVO();
            userVO.setRole(1);
            userVO.setGmId(gridMember.getGmId());
            String token = JwtUtil.newToken(userVO);
            return R.ok("登陆成功")
                    .setResult("gridmember",gridMember)
                    .setResult("token",token);
        }
        return R.error("登录失败");
    }

    @GetMapping("gmsByPidAndCid")
    public R gmsByPidAndCid(@RequestParam Integer provinceId,
                            @RequestParam Integer cityId){
        List<GridMember> gridMembers = gridMemberService.listByPidAndCid(provinceId, cityId);
        return R.ok().setResult("gridMembers",gridMembers);
    }
}
